/*
 * @Description:
 * @Author: Junhuang
 * @Date: 2022-02-17 10:34:36
 * @LastEditTime: 2022-04-09 18:58:53
 * @LastEditors: Junhuang
 * @Reference:
 */
'use strict';

const Controller = require('egg').Controller;
const querystring = require('querystring');
const crypto = require('crypto');

class VodController extends Controller {
  // 签名
  async sign() {
    const { secret_id, secret_key, vodSubAppId } = this.app.config.tencentVod;
    // 确定签名的当前时间和失效时间
    const current = parseInt((new Date()).getTime() / 1000);
    const expired = current + 86400; // 签名有效期：1天

    // 向参数列表填入参数
    const arg_list = {
      secretId: secret_id,
      currentTimeStamp: current,
      expireTime: expired,
      random: Math.round(Math.random() * Math.pow(2, 32)),
      oneTimeValid: 1,
      vodSubAppId,
    };
    // 计算签名
    const orignal = querystring.stringify(arg_list);
    const orignal_buffer = new Buffer(orignal, 'utf8');
    const hmac = crypto.createHmac('sha1', secret_key);
    const hmac_buffer = hmac.update(orignal_buffer).digest();
    const signature = Buffer.concat([ hmac_buffer, orignal_buffer ]).toString('base64');
    console.log('##########signature=', signature);
    this.ctx.apiSuccesss(signature);
  }
}

module.exports = VodController;
